#ifndef _LoRa_MODULE_H
#define _LoRa_MODULE_H
#include "stdint.h"
#include "malloc.h"

typedef struct _LoRa_t lora_t;
typedef uint32_t  (*lora_msg_cb)(lora_t *nf, uint8_t msg, uint8_t arg_int, void *arg_ptr,void *res_arg_ptr);
typedef void     (*nf_set_call)(lora_t *nf);
typedef uint8_t  (*nf_transfer_call)(lora_t *nf,void *arg_ptr,uint8_t arg_int);    



/**************Lora数据结构体*****************/
typedef struct 
{
    uint8_t *data;
    uint8_t len;
}LoRaData_t;

/****************LoRa局域网协议***************/
typedef struct
{
	uint8_t   type;       //包类型
	uint8_t net_id;       //网络ID
	uint16_t source_add;  //源设备地址
	uint16_t target_add;  //目标设备地址
}LoRa_LLP_Hand_t;
/***********LoRa本地网络数据*******************/
typedef struct
{
	LoRa_LLP_Hand_t hand;   //包头信息
	//LoRaData_t      data;   //数据
}LoRa_LLP_t;

/**********LoRa 配置结构体*********/
typedef struct
{
	uint32_t RFFrequency;		         //频率单位 hz
	int8_t Power;				             //发射功率2~20
	uint8_t SignalBw;                //带宽 [0: 7.8 kHz, 1: 10.4 kHz, 2: 15.6 kHz, 3: 20.8 kHz, 4: 31.2 kHz,
                                   //      5: 41.6 kHz, 6: 62.5 kHz, 7: 125 kHz, 8: 250 kHz, 9: 500 kHz, other: Reserved] 带宽
	uint8_t SpreadingFactor;         // 扩频因子  [6: 64, 7: 128, 8: 256, 9: 512, 10: 1024, 11: 2048, 12: 4096  chips] 扩频因子
	uint8_t ErrorCoding;             //误码编码率 [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]	编码器率
	uint16_t PreambleLength;	        //前导码长度
}lora_config_t;
/**************************
* 设备模块状态
**************************/
struct _lora_status
{
	uint8_t RX_FIFO_IsData;      
	uint8_t TX_Complete;
	uint8_t Max_retry;
	uint8_t RX_P_NO;
	uint8_t TX_FIFO_Full;
    uint8_t PRIM_RX;             //发射/接收配置
    uint8_t pwr_up;              //关断开机模式配置
	uint8_t irq;
};
typedef  struct _lora_status   lora_status;
/**************************
 * 底层驱动函数
 * ***********************/
struct _nf_dev_drive
{
	nf_set_call			set_Shutdown_model;//设置关断模式
    nf_set_call			set_tx_model;   //设置发送模式
    nf_set_call			set_rx_model;   //设置接收模式
    nf_transfer_call   tx_Packet;      //发送数据包
    nf_transfer_call   rx_Packet;      //接收数据包
	nf_set_call        irq;            //中断函数
};
typedef  struct _nf_dev_drive   nf_dev_drive;
/*************************
 * 模块结构体
 * **********************/
struct _LoRa_t
{
    lora_config_t       loRa_config;        //模块配置参数
	  lora_status         status;
    lora_msg_cb         byte_cb;          //
    lora_msg_cb         gpio_and_delay_cb;     
};


#define LoRa_MSG_BYTE_SEND                   0x02   //传输数据
#define LoRa_MSG_BYTE_START_TRANSFER         0x01   //传输开始
#define LoRa_MSG_BYTE_END_TRANSFER           0x03   //结束数据传输
#define LoRa_MSG_BYTE_INIT                   0x04   //初始化

#define LoRa_MSG_GPIO_AND_DELAY_INIT         0x05   //延时函数初始化
#define LoRa_MSG_DELAY_MILLI                 0x06    //延时1ms
#define LoRa_MSG_GPIO_CS                     0x07    //CS 引脚            
#define LoRa_MSG_BYTE_Transmit               0x08    //传输数据
#define LoRa_MSG_BYTE_Receive                0x09    //接收数据

#define LoRa_MSG_GPIO_RST                    0x0A    //复位引脚
#define LoRa_MSG_GPIO_ReadDio0               0x0B    //读DIO0引脚
#define LoRa_MSG_GPIO_ReadDio1               0x0C    //读DIO1引脚
#define LoRa_MSG_GPIO_BUSY                   0x0D    //读取BUSY 引脚电平

#define LoRa_MSG_GetTick                     0x020   //获取系统时间

#define LoRa_MSG_GPIO_EXIT_DIO0              0x021    //等待DIO0 中断
#define LoRa_MSG_GPIO_EXIT_DIO1              0x022    //等待DIO1 中断

void LoRa_Setup_SX127X_noname_f(lora_t *nf,lora_msg_cb byte_cb,lora_msg_cb gpio_and_delay);  
#endif
